package com.sys.controller.auth;

import com.sys.common.constant.Constants;
import com.sys.common.core.controller.BaseController;
import com.sys.common.core.domain.AjaxResult;
import com.sys.common.core.domain.entity.SysMenu;
import com.sys.common.core.domain.entity.SysUser;
import com.sys.common.core.domain.model.LoginBody;
import com.sys.common.core.domain.model.RegisterBody;
import com.sys.common.utils.SecurityUtils;
import com.sys.common.utils.StringUtils;
import com.sys.framework.web.service.SysCaptchaImageService;
import com.sys.framework.web.service.SysLoginService;
import com.sys.framework.web.service.SysPermissionService;
import com.sys.framework.web.service.SysRegisterService;
import com.sys.system.domain.fo.BindThirdAuthFO;
import com.sys.system.domain.vo.ThirdAuthVO;
import com.sys.system.service.ISysConfigService;
import com.sys.system.service.ISysMenuService;
import com.sys.system.service.ISysUserThirdAuthService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author LGY
 * Create by 2024/5/28 14:40
 */
@Tag(name = "鉴权API")
@RestController
@RequestMapping("/auth")
public class SysAuthController extends BaseController {

    @Autowired
    private ISysConfigService configService;
    @Autowired
    private SysLoginService loginService;
    @Autowired
    private ISysMenuService menuService;
    @Autowired
    private SysPermissionService permissionService;
    @Autowired
    private SysRegisterService registerService;
    @Autowired
    private SysCaptchaImageService captchaImageService;
    @Autowired
    private ISysUserThirdAuthService sysUserThirdAuthService;

    /**
     * 生成验证码
     */
    @Operation(summary = "获取验证码")
    @GetMapping("/captchaImage")
    public AjaxResult getCode() {
        boolean captchaEnabled = configService.selectCaptchaEnabled();
        if (!captchaEnabled) {
            return AjaxResult.success().put("captchaEnabled", false);
        }
        return captchaImageService.create();
    }

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @Operation(summary = "登录")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @Operation(summary = "获取当前登录用户信息")
    @GetMapping("/getInfo")
    public AjaxResult getInfo() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    @Operation(summary = "获取第三方授权登录地址")
    @GetMapping("/getThirdLoginAuthUrl/{type}")
    public AjaxResult getThirdLoginAuthUrl(@PathVariable("type") String type) {
        return AjaxResult.success(loginService.getThirdLoginAuthUrl(type));
    }

    @Operation(summary = "第三方授权登录")
    @PostMapping("/thirdLogin")
    public AjaxResult thirdLogin(@RequestBody BindThirdAuthFO thirdAuthFO) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.thirdLogin(thirdAuthFO);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @Operation(summary = "获取第三方登录鉴权信息")
    @GetMapping("/getThirdAuths")
    public AjaxResult getThirdAuths() {
        List<ThirdAuthVO> thirdAuthVOList = sysUserThirdAuthService.getThirdAuths(SecurityUtils.getUserId());
        return AjaxResult.success(thirdAuthVOList);
    }

    @Operation(summary = "绑定第三方授权")
    @PostMapping("/bindThirdAuth")
    public AjaxResult bindThirdAuth(@RequestBody BindThirdAuthFO bindThirdAuthFO) {
        sysUserThirdAuthService.bindThirdAuth(bindThirdAuthFO);
        return AjaxResult.success();
    }

    @Operation(summary = "解绑第三方授权")
    @PostMapping("/unbindThirdAuth")
    public AjaxResult unbindThirdAuth(@RequestBody BindThirdAuthFO bindThirdAuthFO) {
        sysUserThirdAuthService.unbindThirdAuth(bindThirdAuthFO);
        return AjaxResult.success();
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @Operation(summary = "获取登录用户路由信息")
    @GetMapping("/getRouters")
    public AjaxResult getRouters() {
        String userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }

    @Operation(summary = "注册")
    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody user) {
        if (!("true" .equals(configService.selectConfigByKey("sys.account.registerUser")))) {
            return error("当前系统没有开启注册功能！");
        }
        String msg = registerService.register(user);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }
}
